//
//  ESDuanziVC.m
//  Eyeshot
//
//  Created by lanou3g on 15/7/1.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESDuanziVC.h"
#import "ESDuanziDetailVC.h"
#import "ESCellTopView.h"
#import "ESDuanzi.h"
#import "ESDuanziTableCell.h"


@interface ESDuanziVC ()
@property (nonatomic, strong) NSMutableArray *models;

@end

@implementation ESDuanziVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTableView];
    [self requestData];
}

#pragma mark - 配置TableView
- (void)configTableView {
    self.tableView.backgroundColor = ESColor(234, 234, 234);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[ESDuanziTableCell class] forCellReuseIdentifier:@"ESDuanziTableCell"];
    _models = [NSMutableArray array];
}

#pragma mark - 网络请求

- (void)requestData {
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    NSString *reqStr = @"http://api.budejie.com/api/api_open.php";
    NSDictionary *reqParas = @{@"a": @"newlist",
                               @"appname": @"baisishequ",
                               @"asid": @"5E6124E7-C271-4F3F-9FE6-33E54108A2F3",
                               @"c": @"data",
                               @"client": @"iphone",
                               @"device": @"iPhone4S",
                               @"from": @"ios",
                               @"jbk": @"0",
                               @"mac": @"",
                               @"market": @"",
                               @"openudid": @"915b9db78a08fe308fdeda8a949ba0f436c6be8f",
                               @"page": @"0",
                               @"per": @"20",
                               @"sub_flag": @"1",
                               @"type": @"29",
                               @"udid": @"",
                               @"uid": @"15103970",
                               @"ver": @"3.6"
                               };
    [mgr GET:reqStr parameters:reqParas success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
        
        NSArray *responseArray = responseObject[@"list"];
        for (NSDictionary *dict in responseArray) {
            ESDuanzi *duanzi = [ESDuanzi objectWithKeyValues:dict];
            [_models addObject:duanzi];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        ESLog(@"%@",error);
    }];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [_models count];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [ESDuanziTableCell heightForCellWithModel:_models[indexPath.row]];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ESDuanziTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ESDuanziTableCell" forIndexPath:indexPath];
    cell.model = _models[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ESDuanziDetailVC *detailVc = [[ESDuanziDetailVC alloc] init];
    detailVc.view.backgroundColor = ESRandomColor;
    [self.navigationController pushViewController:detailVc animated:YES];
}

@end




//- (void)commitData {
//    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json", @"text/json", @"text/javascript", nil];
//
//
//    NSString *reqStr = @"http://api.budejie.com/api/api_open.php";
//    NSDictionary *reqParas = @{@"a": @"love",
//                               @"appname": @"baisishequ",
//                               @"asid": @"5E6124E7-C271-4F3F-9FE6-33E54108A2F3",
//                               @"c": @"post",
//                               @"client": @"iphone",
//                               @"device": @"iPhone4S",
//                               @"id": @"14730455",
//                               @"jbk": @"0",
//                               @"mac": @"",
//                               @"market": @"",
//                               @"openudid": @"915b9db78a08fe308fdeda8a949ba0f436c6be8f",
//                               @"sex": @"m",
//                               @"tj_from": @"video",
//                               @"udid": @"",
//                               @"uid": @"15103970",
//                               @"ver": @"3.6"
//                               };
//    [mgr GET:reqStr parameters:reqParas success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        ESLog(@"%@",responseObject);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        ESLog(@"%@",error);
//    }];
//
//
////
////    NSString *reqStr = @"http://api.budejie.com/api/api_open.php?a=love&appname=baisishequ&asid=5E6124E7-C271-4F3F-9FE6-33E54108A2F3&c=post&client=iphone&device=iPhone%204S&id=14730455&jbk=0&mac=&market=&openudid=915b9db78a08fe308fdeda8a949ba0f436c6be8f&sex=m&tj_from=video&udid=&uid=15103970&ver=3.6";
////
////    NSURL *url = [NSURL URLWithString:reqStr];
////    NSURLRequest *request = [NSURLRequest requestWithURL:url];
////
////    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
////        NSLog(@"%@",data);
////        NSDictionary *getDict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves|NSJSONReadingMutableContainers) error:nil];
////        NSLog(@"%@",getDict);
////    }];
//
//}

